package org.zero.mapper;

import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.zero.domain.UserDO;
import org.zero.provider.SqlProvider;

/**
 * @author Zero
 */
@Mapper
public interface UserMapper {
    /**
     * 查询所有用户
     *
     * @return
     */
    @Select("select * from user")
    List<UserDO> listAll();

    /**
     * 根据id查询用户
     *
     * @param id
     * @return
     */
    @Select("select * from user where id=#{uid}")
    UserDO getById(@Param("uid") int id);

    @SelectProvider(type = SqlProvider.class, method = "select")
    UserDO getById1(int id);

    /**
     * 插入用户
     *
     * @param user
     * @return
     */
    @Insert("insert into user(name,email,gender,birthday) values(#{name},#{email},#{gender},#{birthday})")
    @SelectKey(resultType = int.class, keyProperty = "id", statement = "select last_insert_id()", before = false)
    boolean save(UserDO user);

    @InsertProvider(type = SqlProvider.class, method = "insert")
    boolean save1(UserDO user);

    /**
     * 修改用户
     *
     * @param user
     * @return
     */
    @Update("update user set name=#{name},email=#{email},gender=#{gender},birthday=#{birthday} where id=#{id}")
    boolean update(UserDO user);

    @InsertProvider(type = SqlProvider.class, method = "update")
    boolean update1(UserDO user);

    /**
     * 根据id删除用户
     *
     * @param id
     * @return
     */
    @Delete("delete from user where id=#{uid}")
    boolean remove(int id);

    @InsertProvider(type = SqlProvider.class, method = "delete")
    boolean remove1(int id);
}
